package com.example.farmer.Activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.farmer.Fragment.Add.addFragment;
import com.example.farmer.Fragment.Home.Buyer_HomeFragment;
import com.example.farmer.Fragment.Message.MessageFragment;
import com.example.farmer.Fragment.Mine.MineFragment;
import com.example.farmer.Fragment.ClassFragment;
import com.example.farmer.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;



public class MainActivity extends AppCompatActivity {
    private static final String TAG =MainActivity.class.getSimpleName(); ;
    private Buyer_HomeFragment mHomeFragment;
    private ClassFragment mClassFragment;
    private MessageFragment mOrderFragment;
    private addFragment mtianjiaFragment;
    private MineFragment mMineFragment;
    private BottomNavigationView bottomNavigationView;





    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        //初始化控件
        bottomNavigationView =findViewById(R.id.main_bottom_nv);

        // 检查Intent，如果需要直接显示CircleFragment，则处理
        Intent intent = getIntent();
        if (intent != null && intent.getAction() != null) {
            // 检查Intent的action是否是要显示CircleFragment
            if (intent.getAction().equals(getPackageName() + ".ACTION_SHOW_CIRCLE_FRAGMENT")) {
                showClassFragment();
            }
        }

        //main_bottom_nv tab点击切换
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            int position = item.getItemId() == R.id.shouye ? 0 :
                    item.getItemId() == R.id.Class ? 1 :
                            item.getItemId() == R.id.xiaoxi ? 2 :
                                    item.getItemId() == R.id.wode ? 3 :
                                            4; // tianjia2
            selectedFragment(position);
            return true;
        });


        //默认首页选中

        selectedFragment(0);

    }

    private void showClassFragment() {
        if (findViewById(R.id.content) != null) {
            // 此处应确保CircleFragment尚未实例化，以避免内存泄漏
            if (mClassFragment == null) {
                mClassFragment = new ClassFragment();
                // 此处使用addToBackStack可返回前一个Fragment，但要注意管理栈的深度
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.content, mClassFragment)
                        .addToBackStack(TAG)
                        .commit();
            }else {
                // 如果CircleFragment已经存在，直接显示
                getSupportFragmentManager()
                        .beginTransaction()
                        .show(mClassFragment)
                        .commit();
            }
        }
    }


    public void selectedFragment(int position) {
        //获取fragmentManager管理器
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideFragment(transaction);
        switch (position) {
            case 0:
                if (mHomeFragment == null) {
                    mHomeFragment = new Buyer_HomeFragment();
                    transaction.add(R.id.content, mHomeFragment);
                } else {
                    transaction.show(mHomeFragment);
                }
                break;
            case 1:
                if (mClassFragment == null) {
                    mClassFragment = new ClassFragment();
                    transaction.add(R.id.content, mClassFragment);
                } else {
                    transaction.show(mClassFragment);
                }
                break;
            case 2:
                if (mOrderFragment == null) {
                    mOrderFragment = new MessageFragment();
                    transaction.add(R.id.content, mOrderFragment);
                } else {
                    transaction.show(mOrderFragment);
                }
                break;
            case 3:
                if (mMineFragment == null) {
                    mMineFragment = new MineFragment();
                    transaction.add(R.id.content, mMineFragment);
                } else {
                    transaction.show(mMineFragment);
                }
                break;
            case 4:
                if (mtianjiaFragment == null) {
                    mtianjiaFragment = new addFragment();
                    transaction.add(R.id.content, mtianjiaFragment);
                } else {
                    transaction.show(mtianjiaFragment);
                }
                break;
        }

        //最后设置提交     注意：这句话一定不能少！！！！！！
        transaction.commit();

    }


    private void hideFragment(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            transaction.hide(mHomeFragment);
        }
        if (mMineFragment != null) {
            transaction.hide(mMineFragment);
        }
        if (mClassFragment != null) {
            transaction.hide(mClassFragment);
        }
        if (mOrderFragment != null) {
            transaction.hide(mOrderFragment);
        }
        if (mtianjiaFragment != null) {
            transaction.hide(mtianjiaFragment);
        }


    }
}